package com.hfi.security.app.authentication;

import com.hfi.security.core.exception.ErrorCode;
import com.hfi.security.core.support.Response;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ChangLiang
 * @date 2019/9/8
 */
public class HfiAuthExceptionEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        Response resp = new Response();
        resp.setErrorcode(ErrorCode.NO_AUTHENTICATION.value);
        resp.setValue(ErrorCode.NO_AUTHENTICATION.memo);
        httpServletResponse.setContentType("application/json;charset=utf-8");
        httpServletResponse.getWriter().write(new ObjectMapper().writeValueAsString(resp));
    }
}
